package com.sx.zongzhi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.SpinnerPubAdapter;
import com.sx.zongzhi.bean.EducationBean;
import com.sx.zongzhi.bean.GlobalBean;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.PopulationSearchBean;
import com.sx.zongzhi.bean.PublicBean;
import com.sx.zongzhi.presenter.PublicListPest;
import com.sx.zongzhi.utils.BaseUtil;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IPublicBeanView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HouseholdSearchActivity extends BaseActivity implements IPublicBeanView{

    @BindView(R.id.et_Name) EditText et_Name;
    @BindView(R.id.et_IDCardNum) EditText et_IDCardNum;
    @BindView(R.id.et_Address) EditText et_Address;
    @BindView(R.id.sp_Cancel) Spinner sp_Cancel;//注销状态
    @BindView(R.id.sp_Death) Spinner sp_Death;
    @BindView(R.id.sp_InHome) Spinner sp_InHome;//入户状态
    @BindView(R.id.sp_Sex) Spinner sp_Sex;
    @BindView(R.id.sp_Nation) Spinner sp_Nation;//名族
    @BindView(R.id.sp_Marriage) Spinner sp_Marriage;
    @BindView(R.id.sp_PersonType) Spinner sp_PersonType;//政治面貌
    @BindView(R.id.tv_Education) TextView tv_Education;//学历
    @BindView(R.id.tv_StartTime) TextView tv_StartTime;
    @BindView(R.id.tv_EndTime) TextView tv_EndTime;

    private List<GlobalBean> inHomeState,sexState,nationState, marriageState,personType;
    //当前当事人学历
    private EducationBean.SchoolingsBean schoolingsBean;
    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_household_search);
        ButterKnife.bind(this);
        setTitleName("户籍人口查询");
        setTitleRight(R.string.search);
        initViews();
    }

    private void initViews() {
        PublicListPest  pubList=new PublicListPest(this);
        pubList.getPublicList("人户状态");
        pubList.getPublicList("性别");
        pubList.getPublicList("民族");
        pubList.getPublicList("婚姻状况");
        pubList.getPublicList("政治面貌");

    }

    @OnClick({R.id.tv_right,R.id.tv_StartTime,R.id.tv_EndTime,R.id.tv_Education})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.tv_right:
               submit();
                break;
            case R.id.tv_Education:
                startActivityForResult(new Intent(HouseholdSearchActivity.this,EducationActivity.class),4447);
                break;
            case R.id.tv_StartTime:
                BaseUtil.createDataPop(this,tv_StartTime);
                break;
            case R.id.tv_EndTime:
                BaseUtil.createDataPop(this,tv_EndTime);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 4447://学历
                    schoolingsBean= ( EducationBean.SchoolingsBean)
                            data.getSerializableExtra("SchoolingsBean");
                    tv_Education.setText(schoolingsBean.getName());
                    break;
            }
        }
    }

    private void submit() {
        String name=et_Name.getText().toString().trim();
        String idCardNum=et_IDCardNum.getText().toString().trim();
        String address=et_Address.getText().toString().trim();
        int cancelState=sp_Cancel.getSelectedItemPosition()==0?0:1;
        int deathState=sp_Death.getSelectedItemPosition()==0?0:1;
        GlobalBean hPubBean=(GlobalBean)sp_InHome.getSelectedItem();
        String inHomeId=(hPubBean.getId()).equals("-1") ?"":hPubBean.getId();
        GlobalBean sexBean=(GlobalBean)sp_Sex.getSelectedItem();
        String sexId=(sexBean.getId()).equals("-1") ?"":sexBean.getId();
        GlobalBean nBean=(GlobalBean)sp_Nation.getSelectedItem();
        String nationId=(nBean.getId()).equals("-1") ?"":nBean.getId();
        GlobalBean mBean=(GlobalBean)sp_Marriage.getSelectedItem();
        String marriageId=(mBean.getId()).equals("-1") ?"":mBean.getId();
        GlobalBean perBean=(GlobalBean)sp_PersonType.getSelectedItem();
        String perTypeId=(perBean.getId()).equals("-1") ?"":perBean.getId();
        String schoolings="";
        if (schoolingsBean!=null){
             schoolings=schoolingsBean.getValue();
        }
        String strateTime=tv_StartTime.getText().toString().trim();
        String endTime=tv_EndTime.getText().toString().trim();
        PopulationSearchBean searchBean=new PopulationSearchBean();
        searchBean.setName(name);
        searchBean.setIdCardNo(idCardNum);
        searchBean.setCurrentAddress(address);
        searchBean.setLogOut(cancelState);
        searchBean.setIsDeath(deathState);
        searchBean.setResidentStatus(inHomeId);
        searchBean.setGender(sexId);
        searchBean.setNation(nationId);
        searchBean.setMaritalState(marriageId);
        searchBean.setPoliticalBackground(perTypeId);
        searchBean.setSchoolStr(schoolings);
        searchBean.setBirthday(strateTime);
        searchBean.setEndBirthday(endTime);
        MyRxBus.get().post(new MsgEvent<>("HouseholdActivity",1,searchBean));
        finish();
    }


    @Override
    public void showError(String message) {
        ToastUtils.showToast(this,message);
    }

    @Override
    public void getPublicList(PublicBean publicBean) {
        if ("人户状态".equals(publicBean.getDomainName())){
            inHomeState=publicBean.getDicts();
            inHomeState.add(0,new GlobalBean("未选择","-1"));
            setSpinnerAdapter(sp_InHome,inHomeState);
        }
        if ("性别".equals(publicBean.getDomainName())){
            sexState=publicBean.getDicts();
            sexState.add(0,new GlobalBean("未选择","-1"));
            setSpinnerAdapter(sp_Sex,sexState);
        }
        if ("民族".equals(publicBean.getDomainName())){
            nationState=publicBean.getDicts();
            nationState.add(0,new GlobalBean("未选择","-1"));
            setSpinnerAdapter(sp_Nation,nationState);
        }
        if ("婚姻状况".equals(publicBean.getDomainName())){
            marriageState=publicBean.getDicts();
            marriageState.add(0,new GlobalBean("未选择","-1"));
            setSpinnerAdapter(sp_Marriage,marriageState);
        }
        if ("政治面貌".equals(publicBean.getDomainName())){
            personType=publicBean.getDicts();
            personType.add(0,new GlobalBean("未选择","-1"));
            setSpinnerAdapter(sp_PersonType,personType);
        }
    }

    private void setSpinnerAdapter(Spinner spinner, List<GlobalBean> lists) {
        SpinnerPubAdapter adapter=new SpinnerPubAdapter(this,R.layout.item_text,lists);
        spinner.setAdapter(adapter);
    }
}
